<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../inc.jsp"></jsp:include>
		<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript" charset="utf-8">
		var lastIndex;
		var tempnum = 1;
		$(function() {
		
			var rid = $('#orderReturnForm').find('input[name=rid]');
			var ridCombobox = rid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				onLoadSuccess : function() {
				if('${sessionInfo.repertoryIds}'!=null&&'${sessionInfo.repertoryIds}'!='null'){
					rid.combobox('setValue','${sessionInfo.repertoryIds}');
				}
					parent.$.messager.progress('close');
				},
			    onSelect : function(record){
				    if(record.id!=null){
				    	$.ajax({
							url : '${pageContext.request.contextPath}/serial!noAuth_serial.html',
							data : {type : 'RKTH',rid : record.id},
							type : "POST",
							success : function(d){
								$('#serial').val(d);
							}
						});
				    }
				 }
			});
			var uid = $('#orderReturnForm').find('input[name=uid]');
			var uidCombobox = uid.combobox({
				url : '${pageContext.request.contextPath}/qxgl/userAction!noAuth_userInReperCombobox.html',
				valueField : 'id',
				textField : 'username',
				multiple : false,
				editable : false,
				onLoadSuccess : function() {
					uid.combobox('setValue','${sessionInfo.userId}');
					parent.$.messager.progress('close');
				}
			});
					
			var date = new Date();
			var month = date.getMonth()+1;
			$("#orderDate").val(date.getFullYear()+"-"+month+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes());
			$('#orderDate').datetimebox({  
		    showSeconds:false  
			});	
			
	
			if('${sessionInfo.repertoryIds}'!=null&&'${sessionInfo.repertoryIds}'!='null'){
				$.ajax({
					url : '${pageContext.request.contextPath}/serial!noAuth_serial.html',
					data : {type : 'RKTH',rid : ${sessionInfo.repertoryIds}},
					type : "POST",
					success : function(d){
						$('#serial').val(d);
					}
				});
			}
			$('#dg').datagrid({
				//idField : 'id',
				toolbar:[{
					text: '确定',
					iconCls:'icon-edit',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
					}
				},'-',{
					text: '删除',
					iconCls:'icon-remove',
					handler:function(){
						var row = $('#dg').datagrid('getSelected');
						if (row){
							var index = $('#dg').datagrid('getRowIndex', row);
							$('#dg').datagrid('deleteRow', index);
						}
					}
				},'-',{
					text: '保存',
					iconCls:'icon-add',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
						
						var items = [];
						var rows = $('#dg').datagrid('getRows');
						for ( var i = 0; i < rows.length; i++) {
							items.push(rows[i].id);
							items.push(rows[i].num);
						}
						$('#items').val(items.join(','));
						
						$('#orderReturnForm').form('submit', {
							url : '${pageContext.request.contextPath}/rkgl/orderReturnAction!add.html',
							success : function(d) {
								var json = $.parseJSON(d);
								if (json.success) {
									parent.showTab("添加入库退货单","退货管理");
								}
								parent.sy.messagerShow({
									msg : json.msg,
									title : '提示'
								});
							}
						});
					}
				}],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickRow:function(rowIndex){
					if (lastIndex != rowIndex){
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}else{
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}
					lastIndex = rowIndex;
				}
			});
			
			$(".barcode").live("keypress",function(e) {
				var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
				if (keyCode == 13) {
					_search();
					return false;
				}
			});
		});


		
		function _search() {
			$('#dg').datagrid('endEdit', lastIndex);
			var rid = $('#orderReturnForm').find('input[name=rid]').val();
			$(".rid").attr("name","rid");
			$(".rid").attr("value",rid);
			$('#searchForm').form('submit', {
				url : '${pageContext.request.contextPath}/spgl/goodsAction!noAuth_search_return.html',
				success : function(d) {
					var json = $.parseJSON(d);
					if (json.success==null) {
						if(json.length>1){
							$('#mydialog').show(); 
							$('#mydialog').dialog({ 
							collapsible: true, 
							minimizable: false, 
							maximizable: false,
							closable:false,
							closed:false, 
						}); 
						for(var i=0;i<json.length;i++){
							 $("#mytable").datagrid('appendRow',{
							 
					            barcode:json[i].barcode,
					            type:json[i].type,
					            color:json[i].color,
					            size:json[i].size,
							    /*type:json.type,
							    gtname:json.gtname,
							    bname:json.bname,
							    name:json.name,
							    unit:json.unit,
							    color:json.color,
							    size:json.size,
							    //num:'1'
							    num:tempnum*/
						     });
						}
						$("#mytable").datagrid({
							onDblClickRow:function(rowIndex, rowData){
								searchAddOrderReturn(json,rowIndex);
								$("#mytable").datagrid('loadData',{total:0,rows:[]});					
								$('#mydialog').dialog({
									closed:true,
								 });
								 $("#barcode").val("");
								$("#barcode").focus();
							}
						});
						}else if(json.length>0){
							var rowIndex = 0;
							searchAddOrderReturn(json,rowIndex);
						}
					   
					}else{
						parent.sy.messagerShow({
							msg : json.msg,
							title : '提示'
						});
					}
				}
			});
			$(".rid").attr("name","");
			
			lastIndex = $('#dg').datagrid('getRows').length-1;
			$('#dg').datagrid('selectRow', lastIndex);
		}
		
		function cleanSearch() {
			$('#searchForm input').val('');
		}
		
		function searchAddOrderReturn(json,jsonid){
					var rows = $('#dg').datagrid('getRows');
					var flag = 0;	
				    if(rows.length > 0){		
				        for(var i = 0; i < rows.length; i++) {
				            var tempbarcode = json[jsonid].barcode;
				            if(tempbarcode == rows[i].barcode){	
				                tempnum = rows[i].num + 1;		
				                $('#dg').datagrid('updateRow',{
                                    index: i,
                                    row: {
	                                      num: tempnum
                                    }
                                   });					                
				                flag = 1;
				                tempnum = 1;
				                return false;
				            }					            
					    }					        
				    }
				    if(flag==0){
						$('#dg').datagrid('appendRow',{
							id:json[jsonid].id,
							barcode:json[jsonid].barcode,
							type:json[jsonid].type,
							gtname:json[jsonid].gtname,
							bname:json[jsonid].bname,
							name:json[jsonid].name,
							unit:json[jsonid].unit,
							color:json[jsonid].color,
							size:json[jsonid].size,
							amount:json[jsonid].amount,
							num: tempnum
						});
					}
					return false;					   
				}
					   
		function closeMytableDialog() { 
		
			$("#mytable").datagrid('loadData',{total:0,rows:[]});					
			$('#mydialog').dialog({
				closed:true,
			 });
			$("#barcode").focus();
		} 
	</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
	
		<div data-options="region:'north',border:false" style="height: 135px; overflow: hidden;">
			<form id="orderReturnForm" style="height:95px;">
				<input id="items" name="items" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">日&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;期</th>
						<td style="width: 200px;"><input id="orderDate" name='date'  onClick="WdatePicker()" class='easyui-validatebox' style="width: 200px;" data-options="required:'true',missingMessage:'请选择退货时间'"/></td>
						<th style="width: 80px;">单&nbsp;&nbsp;&nbsp;&nbsp;号</th>
						<td style="width: 200px;"><input name="serial" id="serial" style="width: 200px;" value=""/></td>
					</tr>
					<tr>
						<th style="width: 80px;">退货仓库 </th>
						<td style="width: 200px;"><input name="rid" style="width: 200px;"  data-options="required:'true',missingMessage:'请选择退货擦仓库'"/></td>
						<th style="width: 80px;">操作员</th>
						<td style="width: 200px;"><input name="uid" style="width: 200px;" /></td>
					</tr>
					<tr>
						<th>备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注</th>
						<td colspan="5"><textarea name="remark" style="height:30px;width: 625px;"></textarea></td>
					</tr>
				</table>
			</form>
			
			<form id="searchForm" style="height:10px;margin-top:10px;">
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<input class="rid" type="hidden"/>
						<th style="width: 50px;">商品条码</th>
						<td style="width: 120px;"><input class="barcode" id="barcode" name="barcode" style="width: 200px;" /></td>
						<td style="width: 280px;" align="right">
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">搜索商品</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a>
						</td>
					</tr>
				</table>
			</form>
		</div>
	
		<div data-options="region:'center',border:false" >
			<table id="dg" class="easyui-datagrid" style="height:auto"  data-options="iconCls: 'icon-edit',singleSelect: true">  
		        <thead>
		            <tr>
		            	<th data-options="field:'id',width:100,hidden:true">商品ID</th> 
		            	
		            	<th data-options="field:'barcode',width:120">商品条码</th>  
		                <th data-options="field:'type',width:90">款号</th>  
	                 	<th data-options="field:'name',width:100">商品名称</th> 
	                 	<th data-options="field:'bname',width:90">品牌</th>
		                <th data-options="field:'gtname',width:80,align:'center'">商品类别</th>
		                <th data-options="field:'unit',width:50,align:'center'">单位</th>
                   		<th data-options="field:'color',width:60,align:'center'">颜色</th>
                   		<th data-options="field:'size',width:60,align:'center'">尺码</th>
                   		<th data-options="field:'amount',width:80,align:'center'">库存数</th>
		                <th data-options="field:'num',width:90,align:'center',editor:'numberbox'">退货数</th>
		            </tr>
		        </thead>
		    </table>
		</div>
		<div id="mydialog" style="display:none;padding:5px;width:400px;height:auto;min-height:150px;max-height:200px;overflow-y:scrooll;" title="亲！该条码对应了多件商品，请双击您要退货的商品。">		 		
		<table id="mytable" class="easyui-datagrid" style="height:auto"  data-options="iconCls: 'icon-edit',singleSelect: true"> 
		        <thead>
		            <tr>
		            	<th data-options="field:'barcode',width:100">商品条码</th>  
		            	<th data-options="field:'type',width:100">款号</th>  
	                 	<!-- <th data-options="field:'name',width:100">商品名称</th> 
	                 	<th data-options="field:'bname',width:80">品牌</th> -->
                   		<th data-options="field:'color',width:80,align:'center'">颜色</th>
                   		<th data-options="field:'size',width:80,align:'center'">尺码</th>
		            </tr>
		        </thead>
		    </table>
		</div> 
	</body>
</html>